package cn.mystyle.controller.user;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;

import cn.mystyle.common.bean.BaseController;
import cn.mystyle.common.bean.BaseResponse;
import cn.mystyle.model.user.UserInfo;
import cn.mystyle.service.user.UserService;
import cn.mystyle.util.UserUtil;

/**
 * @author clark
 * 用户控制器
 */
@RestController
@RequestMapping(value = "/user")
public class UserController extends BaseController
{
    /**
     * 日志记录
     */
    private Logger logger = LoggerFactory.getLogger(UserController.class);
    
    /**
     * 用户业务类
     */
    @Autowired
    private UserService userService;
    
    /**
     * 获取用户
     * @param request HTTP 请求体
     * @param response HTTP 响应体
     * @param paramData 请求参数
     * @return String  
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String login(HttpServletRequest request, HttpServletResponse response, @RequestBody String jsonData)
    {
        logger.debug("somenone login now, param:{}", jsonData);
        
        UserInfo user = JSON.parseObject(jsonData, UserInfo.class);
        
        return userService.login(request, user).toString();
    }
    
    /**
     * 注册用户
     * @param request HTTP 请求体
     * @param response HTTP 响应体
     * @param paramData 请求参数
     * @return String  
     */
    @RequestMapping(value = "/signup", method = RequestMethod.POST)
    public String signup(HttpServletRequest request, HttpServletResponse response, @RequestBody String jsonData)
    {
        logger.debug("somenone signup now, param:{}", jsonData);
        
        UserInfo user = JSON.parseObject(jsonData, UserInfo.class);
        
        return userService.signup(request, user).toString();
    }
    
    /**
     * 获取用户
     * @param request HTTP 请求体
     * @param response HTTP 响应体
     * @param paramData 请求参数
     * @return String  
     */
    @RequestMapping(value = "/loginout", method = RequestMethod.POST)
    public String loginout(HttpServletRequest request, HttpServletResponse response, @RequestBody String jsonData)
    {
        logger.debug("somenone login out now, param:{}", jsonData);
        
        UserUtil.seesionLoginOut(request);
        
        return new BaseResponse().toString();
    }
}
